package ws.com.activeMQ.activeMQ;

import com.alibaba.druid.util.StringUtils;
import com.alibaba.nacos.shaded.io.grpc.netty.shaded.io.netty.util.internal.StringUtil;
import lombok.extern.slf4j.Slf4j;
import ws.com.activeMQ.MqConstants;
import ws.com.controller.WebSocketController;
import ws.com.pojo.Barrage;
import ws.com.pojo.WsDTO;
import ws.com.utils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class WsListener {

    @Autowired
    private WebSocketController webSocketController;

    /**
     * 发送弹幕时 ，考虑到websocket集群为分布式 使用mq对其通信
     * @param barrage
     */
    @JmsListener(destination = MqConstants.WS_SEND_BARRAGE)
    public void sendBarrage(String barrage){
        if(StringUtils.isEmpty(barrage) || webSocketController == null){
            log.info("mq消息异常, 消息: {} , 实体类: {}", barrage, webSocketController);
            return;
        }
        Barrage obj = BeanUtils.JSONString2Object(barrage, Barrage.class);
        webSocketController.sendBarrage(obj);
    }

    /**
     * 统计观看人数
     * @param wsDTO
     */
    @JmsListener(destination = MqConstants.WS_WATCH_COUNT)
    public void watchCount(WsDTO wsDTO){
        if(wsDTO == null || webSocketController == null){
            log.info("mq消息异常, 消息: {} , 实体类: {}", wsDTO, webSocketController);
            return;
        }
        webSocketController.countPeople(wsDTO);
    }
}
